<template>
  <el-dialog
    :visible="dialogVisible"
    width="400px"
    :title="editForm?'编辑':'新增'"
    @close="$emit('close')"
  >
    <el-form
      ref="form"
      label-width="120px"
      :model="form"
      :rules="rules"
    >

      <el-form-item
        label="源名称"
        prop="sourceName"
      >
        <el-input
          v-model="form.sourceName"
          placeholder="当前规则的源名称"
          clearable
        />
      </el-form-item>

      <el-form-item
        label="库名称"
        prop="dbName"
      >
        <el-input
          v-model="form.dbName"
          placeholder="当前规则的库名称"
          clearable
        />
      </el-form-item>

      <el-form-item
        label="表名称"
        prop="tbName"
      >
        <el-input
          v-model="form.tbName"
          placeholder="当前规则的表名称"
          clearable
        />
      </el-form-item>

      <el-form-item
        label="库表名"
        prop="dbTbName"
      >
        <el-input
          v-model="form.dbTbName"
          placeholder="当前规则的库表名称"
          clearable
        />
      </el-form-item>
      <el-form-item
        label="源分区"
        prop="sourcePartition"
      >
        <el-input
          v-model="form.sourcePartition"
          placeholder="当前规则的源分区"
          clearable
        />
      </el-form-item>

      <el-form-item
        label="源分区值"
        prop="sourceValue"
      >
        <el-input
          v-model="form.sourceValue"
          placeholder="源分区（多个值逗号隔开）"
          clearable
        />
      </el-form-item>

      <el-form-item
        label="日期分区"
        prop="datePartition"
      >
        <el-input
          v-model="form.datePartition"
          placeholder="当前规则的日期分区"
          clearable
        />
      </el-form-item>
      <el-form-item
        label="保留周期"
        prop="holdDataMonth"
      >
        <el-input
          v-model="form.holdDataMonth"
          placeholder="请输入保留数据（月）"
          clearable
        >
          <template slot="append">月</template>
        </el-input>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        @click="$emit('close')"
      >取 消</el-button>
      <el-button
        type="primary"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import logApi from '@/api/logAnalysis/index'
const form = {
  sourceName: '',
  dbName: '',
  tbName: '',
  dbTbName: '',
  datePartition: '',
  holdDataMonth: '',
  sourcePartition: '',
  sourceValue: ''
}
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    editForm: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      form: {...form},
      rules: {
        sourceName: [
          { required: true, message: '请输入源名称', trigger: 'blur' }
        ],
        dbName: [
          { required: true, message: '请输入库名称', trigger: 'blur' }
        ],
        tbName: [
          { required: true, message: '请输入表名称', trigger: 'blur' }
        ],
        dbTbName: [
          { required: true, message: '请输入库表名称', trigger: 'blur' }
        ],
        datePartition: [
          { required: true, message: '请输入日期分区', trigger: 'blur' }
        ],
        holdDataMonth: [
          { required: true, message: '请输入保留数据（月）', trigger: 'blur' }
        ],
        sourceValue: [
          {pattern: /^[^，]+$/, message: '输入不合规，请用英文逗号分割', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.form = {...form}
        this.$refs.form.resetFields()
      } else {
        if (this.editForm) {
          this.form = this.editForm
        }
      }
    },
    'form.dbName': {
      handler(val) {
        if (!val) {
          return
        }
        this.form.dbTbName = `${this.form.dbName}.${this.form.tbName}`
      }
    },
    'form.tbName': {
      handler(val) {
        if (!val) {
          return
        }
        this.form.dbTbName = `${this.form.dbName}.${this.form.tbName}`
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.editForm) {
            logApi.editTableSyncConf(this.form).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('close')
            })
          } else {
            logApi.addTableSyncConf(this.form).then(res => {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.$emit('close')
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
